package v1

import (
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	v1 "RoccBlog/repository/v1"
	"RoccBlog/service"
	"github.com/gin-gonic/gin"
)

// BusinessTripService 出差业务层
type BusinessTripService struct {
	service.BaseService
	//接口封装业务层CURD
	service.BaseOperationService
	//依赖的持久层
	Repository v1.BusinessTripRepository
}

// 将BusinessTripService注册到全局
func init() {
	global.RegisterService(&BusinessTripService{
		Repository: v1.BusinessTripRepository{},
	})
}

// Add 添加出差
func (s *BusinessTripService) Add(ctx *gin.Context, data model.BusinessTrip) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	//设置添加记录人未当前登录用户
	data.CreateBy = userId

	return s.Repository.Create(data)
}

// Remove 删除出差
func (s *BusinessTripService) Remove(ctx *gin.Context, id int) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.Delete(id, userId)
}

// Modify 修改出差
func (s *BusinessTripService) Modify(ctx *gin.Context, data model.BusinessTrip) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.Update(data, userId)
}

// FindById 查找ById出差
func (s *BusinessTripService) FindById(ctx *gin.Context, id int) (model.BusinessTrip, error) {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return model.BusinessTrip{}, err
	}
	return s.Repository.SelectById(id, userId)
}

// Find 查找列表出差
func (s *BusinessTripService) Find(ctx *gin.Context, params req_resp.BusinessTripReq) (any, error) {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return model.BusinessTrip{}, err
	}
	size, num := s.GetPage(ctx)
	return s.Repository.Select(params, num, size, userId)
}

// FindExpenseByID 查询出差消费
func (s *BusinessTripService) FindExpenseByID(ctx *gin.Context, id, expenseType int) (any, error) {

	size, num := s.GetPage(ctx)
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return nil, err
	}

	return s.Repository.SelectExpense(id, expenseType, size, num, userId)

}

// RemoveExpense 删除出差消费
func (s *BusinessTripService) RemoveExpense(ctx *gin.Context, id int, businessTripID int) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.DeleteExpense(id, businessTripID, userId)
}

// AddExpense 添加出差消费
func (s *BusinessTripService) AddExpense(ctx *gin.Context, expense model.BusinessTripExpense) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	expense.CreateBy = userId
	return s.Repository.CreateExpense(expense)
}
